package com.shqcjd.generalplatformprojectclient.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orhanobut.logger.Logger;
import com.shqcjd.generalplatformprojectclient.R;
import com.shqcjd.generalplatformprojectclient.app.MyApp;
import com.shqcjd.generalplatformprojectclient.model.pojo.GuidanceMsgBean;
import com.shqcjd.generalplatformprojectclient.ui.adapter.GuideMsgAdapter;
import com.shqcjd.generalplatformprojectclient.ui.base.BaseActivity;
import com.shqcjd.generalplatformprojectclient.ui.presenter.FileListPresenter;
import com.shqcjd.generalplatformprojectclient.ui.view.FileListView;
import com.shqcjd.generalplatformprojectclient.util.UIUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;
import io.agora.AgoraAPI;
import io.agora.AgoraAPIOnlySignal;

/**
 * 文件列表页面
 */
public class FileListActivity extends BaseActivity<FileListView, FileListPresenter> implements FileListView {

    @BindView(R.id.btn_back)
    Button btnBack;
    @BindView(R.id.tv_toolbar_center)
    TextView tvToolbarCenter;
    @BindView(R.id.ib_toolbar_right)
    ImageButton ibToolbarRight;
    @BindView(R.id.tv_toolbar_right)
    TextView tvToolbarRight;
    @BindView(R.id.rv_msg_list)
    RecyclerView rvMsgList;
    @BindView(R.id.fl_no_msg)
    FrameLayout flNoMsg;
    private GuideMsgAdapter guideMsgAdapter;
    private String guideId;
    private Context mContext;

    public static final int REFRESH_TYPE_TIME = 4000;
    private Handler handler = new Handler();
    private Runnable r = new Runnable() {
        @Override
        public void run() {
            mPresenter.getFileListDate(guideId);
            handler.postDelayed(r, REFRESH_TYPE_TIME);
        }
    };
    private AgoraAPIOnlySignal mAgoraAPI;

    @Override
    protected FileListPresenter createPresenter() {
        return new FileListPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_file_list;
    }

    @Override
    public void initView() {
        mContext = this;
        mAgoraAPI = MyApp.the().getmAgoraAPI();
        guideId = getIntent().getStringExtra("guideId");
        tvToolbarCenter.setText("远程指导文件");

        guideMsgAdapter = new GuideMsgAdapter(null, this);
        rvMsgList.setLayoutManager(new LinearLayoutManager(this));
        rvMsgList.setAdapter(guideMsgAdapter);
        DefaultItemAnimator animator = new DefaultItemAnimator();
        rvMsgList.setItemAnimator(animator);

//        handler.postDelayed(r, REFRESH_TYPE_TIME);
        mPresenter.getFileListDate(guideId);

        guideMsgAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<GuidanceMsgBean> list = adapter.getData();
                Intent it = new Intent(mContext, UIUtils.previewClass(list.get(position).extension));
                it.putExtra("url", list.get(position).url);
                it.putExtra("name", list.get(position).filename);
                startActivity(it);
            }
        });

    }

    @Override
    public void getDataError(String message) {
        Toasty.info(this, message).show();
    }

    @Override
    public void getRefreshDataSuccess(List<GuidanceMsgBean> data) {
        flNoMsg.setVisibility(data.size() == 0 ? View.VISIBLE : View.GONE);
        guideMsgAdapter.setNewData(data);
    }

    @Override
    public void showProgress(String tipString) {
        showWaitingDialog(tipString);
    }

    @Override
    public void hideProgress() {
        hideWaitingDialog();
    }


    @Override
    protected void onResume() {
        super.onResume();
        addSignalingCallback();
    }

    private void addSignalingCallback() {
        if (mAgoraAPI == null) {
            return;
        }

        mAgoraAPI.callbackSet(new AgoraAPI.CallBack() {

            /**
             * 收到频道消息回调
             * @param channelID 频道名
             * @param account 客户端定义的用户账号
             * @param uid 固定填 0
             * @param msg 消息正文
             */
            @Override
            public void onMessageChannelReceive(String channelID, final String account, final int uid, final String msg) {
                super.onMessageChannelReceive(channelID, account, uid, msg);
                Logger.i("接收到频道消息: account:"+account+" uid:"+uid+" msg:"+msg);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toasty.normal(mContext, "收到新信息").show();
                        mPresenter.getFileListDate(guideId);
                    }
                });
            }

        });
    }

    @OnClick({R.id.btn_back, R.id.tv_toolbar_center})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(handler!=null){
            handler.removeCallbacks(r);
        }
    }
}
